package graphic;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import main.app.LibraryManager;
import net.miginfocom.swing.MigLayout;
import db.Version;

public class LoginInterface extends JFrame {

	private JPanel contentPane;
	private JTextField dbName;
	private JTextField dbPort;
	private JTextField usrname;
	private JTextField usrPassword;
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	
	private LibraryManager manager;
	private Connection newConn = null;
	private String url = "jdbc:postgresql://localhost";
	
	public LoginInterface(LibraryManager libMan) {
		setBackground(Color.DARK_GRAY);
		
		manager = libMan;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 300);
		setLocation((int) (screenSize.getWidth() - this.getWidth()) / 2,(int) (screenSize.getHeight() - this.getHeight()) / 2);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(Color.GRAY);
		contentPane.setLayout(new MigLayout("", "[216px][grow][216px,grow]", "[43px][43px][43px][43px][43px][]"));
		setContentPane(contentPane);
		
		JLabel lblDbConnectionForm = new JLabel("DB connection form");
		lblDbConnectionForm.setHorizontalAlignment(SwingConstants.CENTER);
		lblDbConnectionForm.setBounds(0, 0, 0, 0);
		contentPane.add(lblDbConnectionForm, "cell 0 0 3 1,grow");
		
		JLabel lblDbName = new JLabel("DB name");
		contentPane.add(lblDbName, "cell 0 1,alignx center,growy");
		
		JLabel label = new JLabel("DB port");
		contentPane.add(label, "cell 0 2,alignx center,growy");
		
		JLabel lblUsername = new JLabel("username");
		contentPane.add(lblUsername, "flowx,cell 0 3,alignx center,growy");
		
		JLabel lblPassword = new JLabel("password");
		contentPane.add(lblPassword, "cell 0 4,alignx center,growy");
		
		dbName = new JTextField();
		dbName.setText("libreria");
		dbName.setColumns(10);
		contentPane.add(dbName, "cell 2 1,growx");
		
		dbPort = new JTextField();
		dbPort.setText("default");
		dbPort.setColumns(10);
		contentPane.add(dbPort, "cell 2 2,growx");
				
		usrname = new JTextField();
		usrname.setText("fedez");
		usrname.setColumns(10);
		contentPane.add(usrname, "cell 2 3,growx");
				
		usrPassword = new JTextField();
		usrPassword.setText("any");
		usrPassword.setColumns(10);
		contentPane.add(usrPassword, "cell 2 4,growx");

//		manager.setCredentials(dbName.getText(), dbPort.getText(), usrname.getText(), usrPassword.getText());
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.setHorizontalTextPosition(SwingConstants.CENTER);
		btnConnect.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				url = "jdbc:postgresql://localhost";
				try {
					if (!(dbPort.getText().equalsIgnoreCase("default"))) url = url + ":" + dbPort.getText() + "/" + dbName.getText();
					else url = url + "/" + dbName.getText();
					newConn = DriverManager.getConnection(url, usrname.getText(), usrPassword.getText());
					//TODO successfully connected to DB, start things, set connection to LibraryManager and close this.
					manager.setCredentials(dbName.getText(), dbPort.getText(), usrname.getText(), usrPassword.getText());
					manager.setConnection(newConn);
					manager.setState(true);
				}
				catch (SQLException ex) {
					ErrorMessage er = new ErrorMessage("NOOB you failed to connect!");
					er.setVisible(true);
		            Logger lgr = Logger.getLogger(Version.class.getName());
		            lgr.log(Level.SEVERE, ex.getMessage(), ex);
		        }
				finally {
					if (newConn != null) newConn = null;
				}
				
			}
		});			
		contentPane.add(btnConnect, "cell 0 5 3 1,grow");		
	}

}
